package com.abxh.jetpack.net;

import com.abxh.jetpack.bean.FailData;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import retrofit2.HttpException;

/**
 * Created by dab on 2021/10/14 11:49
 */
public class ErrorUtil {
    public static FailData getErrorData(Throwable e) {
        String message;
        int code;
        if (!NetworkUtils.isConnected()) {
            message = "网络未开启，请检查网络设置";
            code = -1;
        } else if (e instanceof SocketTimeoutException) {
            message = "网络异常，请检查网络设置";
            code = -2;
        } else if (e instanceof ConnectException) {
            message = "服务器异常，请再试";
            code = -3;
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            message = "服务器异常，请再试";
            code = httpException.code();
        } else if (e instanceof ApiException) {
            message = "服务器异常，请再试";
            code = -3;
        }else {
            message = "网络异常，请检查网络设置";
            code = -2;
        }
        return new FailData(code, message);
    }

    public static String getErrorMessage(Throwable e) {
        return getErrorData(e).getMessage();
    }

    public static int getErrorCode(Throwable e) {
        return getErrorData(e).getCode();
    }

}
